import store from "../vuex/store";
import {AuthConfig} from "../Contants";
import {getCurrentUserInfo} from "~/interface/user";
import {MUTATION_UPDATE_USER_INFO} from "~/vuex/auth";

export default {
    hasAuth(auth, userInfo = store.state.auth.userInfo){
        if(!AuthConfig.authEnable) return true;
        return Boolean(userInfo.authorizationEntityList.find(a => a.name === auth));
    },
    /**
     * 自动通过cookie进行用户登陆
     */
    async autoLogin() {
        if(!store.state.auth.hasLogin){
            try {
                const info = await getCurrentUserInfo();
                if(info) store.commit(MUTATION_UPDATE_USER_INFO, { userInfo: info, loginSuccess: true })
                return Boolean(info);
            }catch (e) {
                return false;
            }
        }
        return true;
    },
}
